package br.upe.dsc.caeto.core.application;

import br.upe.dsc.caeto.core.IRegister;
import br.upe.dsc.caeto.core.IRepository;
import br.upe.dsc.caeto.core.InvalidElementException;
import br.upe.dsc.caeto.core.ElementNotFoundException;
import br.upe.dsc.caeto.simulation.SimulationContext;
import java.util.Iterator;

public class ApplicationRegister implements IRegister<Application> {
	private IRepository<Application> repository;
	
	public ApplicationRegister (IRepository repository) {
		this.repository = repository;
	}
	
	public void add(Application application) throws InvalidElementException {
		//if (context.validateHierarchy(application.getHierarquia())){
			this.repository.insert(application);
		//}
		//else {
			//throw new InvalidElementException("Application " +
				//	application.getName() + " is not valid.");
		//}
	}
	
	public void remove(Application application) throws
		ElementNotFoundException {
		
		if (!this.repository.isElement(application.getName())) {
			throw new ElementNotFoundException("Application " +
					application.getName() + " not found.");
		}
		else{
			this.repository.remove(application);
		}
	}
	
	public void update(Application application) throws
		ElementNotFoundException {
		/*if (!this.repositorio.existe(aplicacao.getName())) {
			throw new AplicacaoInexistenteException();
		}
		else {*/
			this.repository.update(application);
		//}
	}
	
	public Application search(String name) throws ElementNotFoundException {
		if (this.repository.search(name) == null ){
			throw new ElementNotFoundException("Application " + name +
					" not found.");
		}
		else{
			return this.repository.search(name);
		}
	}
	
	public boolean isElement(String name) {
		return this.repository.isElement(name);
	}
	
	public void empty() {
		this.repository = null;
	}
	
	public Iterator<Application> iterator() {
		return this.repository.iterator();
	}
	
	public Iterable<Application> getElements() {
		return this.repository.getElements();
	}
}
